package cn.bravolinks.tools.controller;

import cn.bravolinks.tools.domain.User;
import cn.bravolinks.tools.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by yanqin on 2017/6/4.
 */
@Controller
@RequestMapping("admin")
public class AdminController extends BaseController {

    @Autowired
    private UserService userService;

    @RequestMapping("/")
    public String adminPage(){
        return "admin";
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @RequestMapping(value = "insert",method = RequestMethod.POST)
    @ResponseBody
    public String insert(User user){
        if(!StringUtils.isEmpty(user.getUserid()) && !StringUtils.isEmpty(user.getUsername()) && !StringUtils.isEmpty(user.getPassword()) && !StringUtils.isEmpty(user.getUserType())) {
            int i = userService.getByuserid(user.getUserid());
            if(i==0) {
                i = userService.insert(user);
                if (i > 0) {
                    return "添加成功";
                } else {
                    return "添加失败";
                }
            }else{
                return "该用户ID已经存在";
            }
        }
        return "信息不符合要求";
    }
}
